import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'enums.dart';

class Utils {
  var bbq = 0;
  //验证手机号
  static bool isChinaPhoneLegal(String str) {
    bool one = RegExp(
        '^[1][345789][0-9]{9}\$').hasMatch(str);
    bool two = RegExp(
        '^[0][1-9][0-9]{1,2}[2-9][0-9]{6,7}\$')
        .hasMatch(str);
    return (one||two);
  }
//验证邮箱
  static bool isEmailLegal(String str) {
    return new RegExp(
        '^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+\$')
        .hasMatch(str);
  }

  //格式化时间到天：yyyy-mm-dd
  static String formatDateTimeToDay(String date,{dynamic type:DateType.utcDate}){
    var temTime;
    if(date==''||date==null){
      return '--';
    }else if(type==DateType.utcDate){
      temTime = DateTime.parse("${date.substring(0,19)}-0800");
    }else{
      temTime = DateTime.parse(date);
    }
    return formatDate(temTime, [yyyy, "-", mm, "-", dd]);
  }

  //格式化时间到分钟：yyyy-mm-dd HH:nn
  static String formatDateTimeToMinute(String date,{dynamic type:DateType.utcDate}){
//    var temTime = DateTime.parse(date);

  // if(date==''||date==null){
  //   return '--';
  // }
  var temTime;
  if(date==''||date==null){
    return '--';
  }else if(type==DateType.utcDate){
    temTime = DateTime.parse("${date.substring(0,19)}-0800");
  }else{
    temTime = DateTime.parse(date);
  }
  // return formatDate(temTime, [yyyy, "-", mm, "-", dd]);
  //   var temTime = DateTime.parse("${date.substring(0,19)}-0800");
    return formatDate(temTime, [yyyy, "-", mm, "-", dd, " ", HH, ":", nn]);
  }

  //格式化时间到秒：yyyy-mm-dd HH:nn:ss
  static String formatDateTimeToSecond(String date,{dynamic type:DateType.utcDate}){
//    var temTime = DateTime.parse(date);
  var temTime;
    if(date==''||date==null){
      return '--';
    }else if(type==DateType.utcDate){
      temTime = DateTime.parse("${date.substring(0,19)}-0800");
    }else{
      temTime = DateTime.parse(date);
    }
    return formatDate(temTime, [yyyy, "-", mm, "-", dd, " ", HH, ":", nn, ":", ss]);
  }

  //格式化时间到秒：yyyy-mm-dd HH:nn:ss
  static String formatDateTimeToSecond2(String date,{dynamic type:DateType.utcDate}){
//    var temTime = DateTime.parse(date);
    var temTime;
    if(date==''||date==null){
      return '--';
    }else if(type==DateType.utcDate){
      temTime = DateTime.parse("${date.substring(0,19)}-0800");
    }else{
      temTime = DateTime.parse(date);
    }
    return formatDate(temTime, [yyyy, "-", mm, "-", dd, "T", HH, ":", nn, ":",ss,'.',SSS,Z]);
  }

  //时间转化年-月-日
  static String timeUtil(time) {
    if(time==null||time==''){
      return '--';
    }
    var temTime = DateTime.parse(time);
    return temTime.year.toString() +
        '-' +
        (temTime.month > 9
            ? temTime.month.toString()
            : ('0' + temTime.month.toString())) +
        '-' +
        (temTime.day > 9
            ? temTime.day.toString()
            : ('0' + temTime.day.toString()));
  }
  //时间转化年-月-日 时分秒
  static String dateTimeUtil(time) {
    if(time==null){
      return '';
    }
    var temTime = DateTime.parse(time);
    return temTime.year.toString() +
        '-' +
        (temTime.month > 9
            ? temTime.month.toString()
            : ('0' + temTime.month.toString())) +
        '-' +
        (temTime.day > 9
            ? temTime.day.toString()
            : ('0' + temTime.day.toString()))+' '+(temTime.hour > 9
        ? temTime.hour.toString()
        : ('0' + temTime.hour.toString()))+':'+(temTime.minute > 9
        ? temTime.minute.toString()
        : ('0' + temTime.second.toString()))+':'+(temTime.hour > 9
        ? temTime.second.toString()
        : ('0' + temTime.second.toString()));
  }

  static int formatDateDifferent(String date1,String date2){
    var temTime1 = DateTime.parse(date1);
    var temTime2 = DateTime.parse(date2);
    final difference = temTime2.difference(temTime1).inHours;
    return difference;
  }
  static handleDate(String oldTime) {
    String nowTime = new DateTime.now().toString().split('.')[0];

    int nowyear = int.parse(nowTime.split(" ")[0].split('-')[0]);
    int nowmonth = int.parse(nowTime.split(" ")[0].split('-')[1]);
    int nowday = int.parse(nowTime.split(" ")[0].split('-')[2]);
    int nowhour = int.parse(nowTime.split(" ")[1].split(':')[0]);
    int nowmin = int.parse(nowTime.split(" ")[1].split(':')[1]);

    int oldyear = int.parse(oldTime.split(" ")[0].split('-')[0]);
    int oldmonth = int.parse(oldTime.split(" ")[0].split('-')[1]);
    int oldday = int.parse(oldTime.split(" ")[0].split('-')[2]);
    int oldhour = int.parse(oldTime.split(" ")[1].split(':')[0]);
    int oldmin = int.parse(oldTime.split(" ")[1].split(':')[1]);

    var now = new DateTime(nowyear, nowmonth, nowday, nowhour, nowmin);
    var old = new DateTime(oldyear, oldmonth, oldday, oldhour, oldmin);
    var difference = now.difference(old);

    if(difference.inDays > 1) {
      return (difference.inDays).toString() + '天前';
    } else if(difference.inDays == 1) {
      return '昨天'.toString();
    } else if(difference.inHours >= 1 && difference.inHours < 24) {
      return (difference.inHours).toString() + '小时前';
    } else if(difference.inMinutes > 5 && difference.inMinutes < 60) {
      return (difference.inMinutes).toString() + '分钟前';
    } else if(difference.inMinutes <= 5) {
      return '刚刚';
    }
  }

  /*
  * 比较时间
  * */
  static differentTime(String newTime,String oldTime) {
    if(newTime==''||newTime==null||oldTime==''||oldTime==null){
      return true;
    }
    var now = DateTime.parse(newTime);
    var old = DateTime.parse(oldTime);
    var difference = old.difference(now);
    return difference.inMinutes<0;
  }

  static utilsTime(String time){
    String  onTime = '';
    if(time!=null&&time!=''){
      DateTime createTime = DateTime.fromMillisecondsSinceEpoch(int.parse(time));
      onTime = createTime.toLocal().toString().substring(0,19);
    }
    return onTime;
  }


  static showAlert(context){
    showDialog(
      context: context,
      barrierDismissible: false, //// user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('移库成功'),
          content: Text('货位发生改变，请重新打印二维码'),
          actions: <Widget>[
            FlatButton(
              child: Text('知道了'),
              onPressed: () {
                Navigator.pop(
                  context,
                );
                Navigator.popUntil(context, ModalRoute.withName('relocation_list_page'));
              },
            ),
          ],
        );
      },
    );
  }



  /*
  * 拼接带层级字符串
  * */
  static String formatHierarchy(String str1,String str2,[String str3='']){
    if(str1==null){
      str1 = '--';
    }
    if(str2 == null){
      str2 = '--';
    }
    if(str3 != null&&str3!=''){
      return str1+'/'+str2+'/'+str3;
    }else{
      return str1+'/'+str2;
    }
  }
/*
  * 拼接不带层级字符串
  * */
  static String formatNotHierarchy(String str1,String str2){
    if(str1==null){
      str1 = '--';
    }
    if(str2 == null){
      str2 = '--';
    }
    return str1+'|'+str2;
  }

  //拼接时间段
  static String formatTwoTime(String str1,String str2){
    if(str1==null){
      str1 = '--';
    }else{
      str1 = formatDateTimeToMinute(str1);
    }
    if(str2 == null){
      str2 = '--';
    }else{
      str2 = formatDateTimeToMinute(str2);
    }
    return str1+'~'+str2;
  }

// 调用随机数方法
  static getRandomCode(number){
    var code = '';
    String alphabet = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ';
    for (var i = 0; i < number; i++) {
      String charOrNum = Random().nextInt(2) % 2 == 0 ? "char" : "num";
      switch (charOrNum) {
        case "char":
          code += alphabet[Random().nextInt(alphabet.length)];
          break;
        case "num":
          code += Random().nextInt(9).toString();
          break;
      }
    }
    return code;
  }

  static String diffTime(String str) {

   var allHours = DateTime.parse(str).difference(DateTime.now()).inHours.toString();
   num days =int.parse(allHours)~/24.toInt();
   num hours = int.parse(allHours)%24;
    return (days<0?'已逾期'+(-days).toString():days.toString())+'天'+hours.toString()+'小时';
  }


  static getPrintSize( limit){
    String  size = "";
    //内存转换
    if(limit < 0.1 * 1024){                            //小于0.1KB，则转化成B
      size = limit.toString();
      size = size.substring(0,size.indexOf(".")+3) + "  B";
    }else if(limit < 0.1 * 1024 * 1024){            //小于0.1MB，则转化成KB
      size = (limit/1024).toString();
      size = size.substring(0,size.indexOf(".")+3) + "  KB";
    }else if(limit < 0.1 * 1024 * 1024 * 1024){        //小于0.1GB，则转化成MB
      size = (limit/(1024 * 1024)).toString() ;
      print(size.indexOf("."));
      size = size.substring(0,size.indexOf(".")+3) + "  MB";
    }else{                                            //其他转化成GB
      size = (limit/(1024 * 1024 * 1024)).toString();
      size = size.substring(0,size.indexOf(".")+3) + "  GB";
    }
    return size;
  }

  static getPunishmentType(value){
    if(value!=null&&value!=''){
      switch (value){
        case '1':
          return '警告';
          break;
        case '2':
          return '停工';
          break;
        case '3':
          return '留用察看';
          break;
        case '4':
          return '清除出场';
          break;
        default:
          return '';
      }
    }else{
      return '';
    }
  }
}



